package org.example.iris.controller;

import org.example.iris.mapper.StudentFileMapper;
import org.example.iris.entity.File;
import org.example.iris.dto.Result;
import org.example.iris.entity.Student;
import org.example.iris.service.DownLoadLogService;
import org.example.iris.service.FileService;
import org.example.iris.service.ResumeFileService;
import org.example.iris.service.StudentService;
import org.example.iris.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    StudentService studentService;

    @Autowired
    StudentFileMapper studentFileMapper;

    @Autowired
    FileService fileService;

    @Autowired
    ResumeFileService resumeFileService;

    @Autowired
    DownLoadLogService downLoadLogService;

    @GetMapping("getResumeFile")
    public Result<List<Map<Integer, String>>> getResumeFile() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        Student student = studentService.findByUserId(userId);
        List<Map<Integer, String>> files = studentFileMapper.findFilesByStudentId(student.getStudentId());

        return Result.success(files);
    }

    @PostMapping("uploadFile")
    public Result uploadFile(MultipartFile file) throws Exception {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }
        String url = fileService.uploadFile(file,  userId);
        return Result.success(url);
    }

    @DeleteMapping("/deleteFile")
    public Result deleteFile(Integer fileId) {

        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");
        String role = (String) claims.get("role");
        if (!role.equals("student")) {
            return Result.error("无权限");
        }

        Student student = studentService.findByUserId(userId);
        fileService.deleteFile(student.getStudentId(), fileId);
        return Result.success();
    }

    @GetMapping("/downloadFile")
    public Result downloadFile(Integer fileId) {

        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("userId");

        File file = resumeFileService.findById(fileId);
        if (file == null) {
            return Result.error("文件不存在");
        }
        resumeFileService.updateFile(file);
        downLoadLogService.insertDownLog(userId, fileId);
        return Result.success(file.getFilePath());
    }
}
